<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <!-- <a-button type="primary" @click="handleDownload">下载文章</a-button> -->
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            // {
            //   label: '查看',
            //   icon: 'ant-design:eye-outlined',
            //   onClick: handleView.bind(null, record),
            // },
            {
              label: '下载',
              icon: 'ant-design:download-outlined',
              onClick: handleDownload.bind(null, record),
            },
          ]"
        />
      </template>
      <template #image="{ record }">
        <div class="article-image">
          <Image :height="50" :src="record.cover" />
        </div>
      </template>
      <template #account="{ record }">
        <a @click="handleAccount(record)">{{ record.a_nickname }}</a>
      </template>
      <template #link="{ record }">
        <a :href="record.link" target="_blank" rel="noreferrer">{{ record.title }}</a>
      </template>
    </BasicTable>
  </div>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';
  import { Image } from 'ant-design-vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useGo } from '/@/hooks/web/usePage';
  import { getArticleList, downloadArticle } from '/@/api/demo/wechat';

  import { columns, searchFormSchemaIndex } from './data';

  export default defineComponent({
    name: 'WechatArticle',
    components: { BasicTable, TableAction, Image },
    setup() {
      const go = useGo();
      const { createMessage } = useMessage();
      const [registerTable, { reload }] = useTable({
        title: '公众号文章',
        api: getArticleList,
        rowKey: 'id',
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchemaIndex,
          autoSubmitOnEnter: true,
        },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: true,
        // rowSelection: {
        //   type: 'checkbox',
        // },
        actionColumn: {
          width: 200,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
        },
      });

      async function handleDownload(record: Recordable) {
        console.log('下载');
        //await downloadArticle({ id: record.id });
        createMessage.warning('功能开发中');
      }

      function handleAccount(record: Recordable) {
        go('/wechat/article/' + record.a_nickname + '/' + record.a_fakeid);
      }
      function handleView(record: Recordable) {
        console.log(record.link);
        window.open(record.link, '_blank');
      }

      return {
        registerTable,
        handleDownload,
        handleAccount,
        handleView,
      };
    },
  });
</script>
<style lang="less" scoped>
  .article-image {
    :deep(.ant-image-img) {
      width: auto;
      height: 100%;
    }
  }
</style>
